package jjn.carl.greedy;

import java.util.Scanner;

/**
 * @author Jjn
 * @since 2023/8/1 12:03
 */
public class LeetCode134 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int totalSum = 0, currentSum = 0;
        int start = 0;
        for (int i = 0; i < gas.length; i++) {
            totalSum += (gas[i] - cost[i]);
            currentSum += (gas[i] - cost[i]);
            if (currentSum < 0) {
                currentSum = 0;
                start = i + 1;
            }
        }
        if (totalSum < 0) {
            return -1;
        }
        return start;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        int[] gas = new int[count];
        int[] cost = new int[count];
        for (int i = 0; i < count; i++) {
            gas[i] = scanner.nextInt();
        }
        for (int i = 0; i < count; i++) {
            cost[i] = scanner.nextInt();
        }
        int canCompleteCircuit = new LeetCode134().canCompleteCircuit(gas, cost);
        System.out.println(canCompleteCircuit);
    }
}
